package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl;

import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.CorrectionFactor;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.CorrectionFactorRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023/8/15 19:49
 * @Description:
 */
@Repository
public class CorrectionFactorRepositoryImpl implements CorrectionFactorRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveCorrectionFactor(CorrectionFactor entity) {
        String sql = "insert into xczh_correction_factor(xcf_id,xsem_id,xsem_name,factor1,factor2,factor3," +
                "factor4,factor5,factor6,del_flag,create_user,create_time,extends1," +
                "extends2,extends3,factor_type1,factor_type2) " +
                "values(:xcfId,:xsemId,:xsemName,:factor1,:factor2,:factor3,:factor4,:factor5,:factor6," +
                ":delFlag,:createUser,:createTime,:extends1,:extends2,:extends3,:factorType1,:factorType2)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);

    }

    @Override
    public void updateCorrectionFactor(CorrectionFactor entity) {
        String sql="update xczh_correction_factor set xsem_id=:xsemId,factor1=:factor1," +
                "factor2=:factor2,factor3=:factor3,factor4=:factor4,factor5=:factor5,factor6=:factor6," +
                "factor_type1=:factorType1,factor_type2=:factorType2," +
                "update_user=:updateUser,update_time=:updateTime " +
                " where xsem_id=:xsemId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public CorrectionFactor getCorrectionFactorByXsemId(String xsemId) {
        String sql="select * from xczh_correction_factor where xsem_id = ? ";
        List<CorrectionFactor> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CorrectionFactor.class), xsemId);
        return query.size()<1?null:query.get(0);
    }
}
